﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using VitalShining.WebControls;

namespace CARMS.Email
{
    [Function("Admin.EmailTaskList")]
    public partial class EmailTaskList : PageBase
    {
        
        private Guid CurrentJobID
        {
            get
            {
                return GetGuidByParameter("JobID");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //LoadDefault();
            }
        }

        public void LoadDefault()
        {
            PageQueryAdapter1.PerformQuery();
        }

        protected void gridList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var entity = (MailTaskEntity) e.Row.DataItem;
                var btnViewEmail = e.Row.FindControl("btnViewEmail") as ImageButton;
                btnViewEmail.Attributes.Add("onclick","OpenMailViewWindow('"+entity.MailId+"');return false;");
            }
        }

        protected void gridList_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {

            }
        }


        protected void PageQueryAdapter1_PerformGetRecordCount(object sender, PerformGetRecordCountEventArgs e)
        {

            var collection = new MailTaskCollection();
            e.RecordCount = collection.GetDbCount((IPredicate)e.Condition);
        }

        protected void PageQueryAdapter1_PerformGetCondition(object sender, PerformGetConditionEventArgs e)
        {
            PredicateExpression searchExpression = new PredicateExpression();
            //if (txtReferenceNo1.Text.Trim().Length > 0)
            //{
            //    var jobCollection = new JobCollection();
            //    jobCollection.GetMulti(JobFields.RegistrationNumber % ("%" + txtReferenceNo1.Text.Trim() + "%"));
            //    var listJobID = from item in jobCollection select item.JobId;
            //    var fileCompare = new FieldCompareRangePredicate(MailTaskFields.JobId, false, listJobID.ToArray());

            //    searchExpression.AddWithAnd(fileCompare);

            //}

            //if (!string.IsNullOrEmpty(txtFromDate1.Text.Trim()))
            //{
            //    searchExpression &= MailTaskFields.CreateDate >= DateTime.Parse(txtFromDate1.Text, ApplicationConfiguration.DateTimeFormatInfo).Date;
            //}
            //if (!string.IsNullOrEmpty(txtToDate1.Text.Trim()))
            //{
            //    searchExpression &= MailTaskFields.CreateDate < DateTime.Parse(txtToDate1.Text, ApplicationConfiguration.DateTimeFormatInfo).AddDays(1).Date;
            //}

            //searchExpression.AddWithAnd(MailTaskFields.IsActive == true);
            //var empID = SecurityBusiness.GetEmployeeIDByUserID(CurrentLoginUser.UserID);
            //if (empID.HasValue)
            //{
            //    searchExpression.AddWithAnd(MailTaskFields.EmployeeId == empID.Value);
            //}
            //else
            //{
            //    searchExpression.AddWithAnd(MailTaskFields.EmployeeId == Guid.Empty);
            //}
            //searchExpression.AddWithAnd(MailTaskFields.JobId == CurrentJobID);
            e.Condition = searchExpression;
        }


        protected void PageQueryAdapter1_PerformGetPageData(object sender, PerformGetPageDataEventArgs e)
        {
            SortExpression sort;
            switch (gridList.SortExpression)
            {
            
                case "Subject":
                    sort = new SortExpression(MailTaskFields.Subject | (SortOperator)(int)gridList.SortDirection);
                    break;
                case "MailTo":
                    sort = new SortExpression(MailTaskFields.MailTo | (SortOperator)(int)gridList.SortDirection);
                    break;
                case "MailCc":
                    sort = new SortExpression(MailTaskFields.MailCc | (SortOperator)(int)gridList.SortDirection);
                    break;
                case "Status":
                    sort = new SortExpression(MailTaskFields.Status | (SortOperator)(int)gridList.SortDirection);
                    break;
             
                case "CreateDate":
                    sort = new SortExpression(MailTaskFields.CreateDate | (SortOperator)(int)gridList.SortDirection);
                    break;
                default:
                    sort = new SortExpression(MailTaskFields.CreateDate | SortOperator.Descending);
                    break;
            }
 
            var collection = new MailTaskCollection();
            collection.GetMulti((IPredicate)e.Condition, 0,
                                sort,
                                null,
                                e.PageIndex + 1, e.PageSize);
            e.DataSource = collection;
        }

    }
}
